package com.tjb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjb.reggie.common.BaseContext;
import com.tjb.reggie.common.R;
import com.tjb.reggie.entity.OrderDetail;
import com.tjb.reggie.entity.Orders;
import com.tjb.reggie.entity.dto.OrdersDto;
import com.tjb.reggie.service.OrderDetailService;
import com.tjb.reggie.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tjb
 * @version 1.0
 * @date 2022/8/26 1:05
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService detailService;
    @GetMapping("page")
    public R<Page<Orders>> page(int page, int pageSize, String number, String beginTime, String endTime){
        //基本信息查询
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getOrderTime).orderByDesc(Orders::getCheckoutTime);
        wrapper.like(number != null, Orders::getNumber, number);
        wrapper.between((beginTime != null || endTime != null), Orders::getOrderTime, beginTime, endTime);
        orderService.page(ordersPage, wrapper);
        //返回
        return R.success(ordersPage);
    }
    /**
     * 用户下单
     */
    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 用户的历史订单,分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize){
        //根据用户的id，查询用户的历史订单,基本信息
        Long userId = BaseContext.getCurrentUserId();
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, userId);
        wrapper.orderByDesc(Orders::getOrderTime).orderByDesc(Orders::getCheckoutTime);
        orderService.page(ordersPage, wrapper);
        //详细信息封装
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        //赋值
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        //获取值
        List<Orders> records = ordersPage.getRecords();
        //获取订单详细下面的值封装
        List<OrderDetail> orderDetails = detailService.list();
        //操作封装的值
        List<OrdersDto> ordersDtos = records.stream().map((item) -> {
            //封装返回值
            OrdersDto dto = new OrdersDto();
            //先赋值
            BeanUtils.copyProperties(item, dto);
            //创建一个详细信息的集合
            List<OrderDetail> details = new ArrayList<>();
            //操作详细信息，对比，然后添加详细信息进入集合
            for (OrderDetail orderDetail : orderDetails) {
                if (item.getNumber().equals(Long.toString(orderDetail.getOrderId()))){
                    details.add(orderDetail);
                }
            }
            //设置值
            dto.setOrderDetails(details);
            //返回
            return dto;
        }).collect(Collectors.toList());
        //赋值
        ordersDtoPage.setRecords(ordersDtos);
        //返回
        return R.success(ordersDtoPage);
    }

    /**
     * 更新订单
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("更新成功");
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("again")
    public R<String> again(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("再来一单下单成功");
    }
}
